package com.itany.ems.servlet;

import com.itany.ems.entity.Emp;
import com.itany.ems.exception.RequestParameterErrorException;
import com.itany.ems.exception.ServiceException;
import com.itany.ems.service.EmpService;
import com.itany.ems.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/3 14:02
 * Version:1.0-SNAPSHOT
 */
@WebServlet("*.emp")
public class EmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/add.emp".equals(path)){
            addEmp(request,response);
        }
        if("/findAll.emp".equals(path)){
            findAll(request,response);
        }
        if("/findById.emp".equals(path)){
            findById(request,response);
        }
        if("/modify.emp".equals(path)){
            modifyEmp(request,response);
        }
        if("/remove.emp".equals(path)){
            removeEmp(request,response);
        }
    }

    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");
        try {
            Emp emp = empService.findById(id);
            request.setAttribute("emp",emp);
            request.getRequestDispatcher("/jsp/modifyEmp.jsp").forward(request,response);
        } catch (RequestParameterErrorException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("empMsg","服务器内部异常");
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        }
    }

    public void modifyEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String salary = request.getParameter("salary");
        String gender = request.getParameter("gender");
        String edu = request.getParameter("edu");
        String[] hobbies = request.getParameterValues("hobbies");

        try {
            empService.modifyEmp(id,name,salary,gender,edu,hobbies);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/modifyEmp.jsp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/modifyEmp.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("empMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/modifyEmp.jsp").forward(request,response);
        }

    }

    public void removeEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String id = request.getParameter("id");

        try {
            empService.removeEmp(id);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("empMsg","服务器内部异常");
            request.getRequestDispatcher("/findAll.emp").forward(request,response);
        }
    }

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();

        try {
            List<Emp> emps = empService.findAll();
            request.setAttribute("emps",emps);
        } catch (ServiceException e) {
            request.setAttribute("empMsg",e.getMessage());
        } catch (Exception e) {
            request.setAttribute("empMsg","服务器内部异常");
        }

        request.getRequestDispatcher("/jsp/empList.jsp").forward(request,response);
    }

    public void addEmp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EmpService empService = new EmpServiceImpl();
        String name = request.getParameter("name");
        String salary = request.getParameter("salary");
        String gender = request.getParameter("gender");
        String edu = request.getParameter("edu");
        String[] hobbies = request.getParameterValues("hobbies");

        try {
            empService.addEmp(name,salary,gender,edu,hobbies);
            response.sendRedirect(request.getContextPath()+"/findAll.emp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/addEmp.jsp").forward(request,response);
        } catch (ServiceException e) {
            request.setAttribute("empMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/addEmp.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("empMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/addEmp.jsp").forward(request,response);
        }

    }

}
